package ecoll.client.dictionary;

import java.util.ArrayList;
import java.util.Arrays;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class WordNet implements Dictionary {

	private ArrayList<Definition> definitions = new ArrayList<Definition>();

	public void getDefinition(final String word, final Panel definitionBox) {

		String page = new String(
				"http://wordnetweb.princeton.edu/perl/webwn?s="
						+ word.replace(' ', '+').toLowerCase()
						+ "&sub=Search+WordNet&o2=&o0=1&o8=1&o1=1&o7=&o5=&o9=&o6=&o3=&o4=&h=");

		page = URL.encodeQueryString(page);

		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
				GWT.getModuleBaseURL() + "proxy?page=" + page);

		try {
			builder.sendRequest(null, new RequestCallback() {
				public void onError(Request request, Throwable e) {
					Window.alert(e.getMessage());
				}

				public void onResponseReceived(Request request,
						Response response) {
					if (200 == response.getStatusCode()) {
						final String PART_START = "<h3>";
						final String PART_END = "</h3>";
						String definition = response.getText();
						definition = definition.substring(definition
								.indexOf(PART_START) + PART_START.length());
						definition = definition.replaceAll("<b>.*</b>,*", "");

						String[] parts = definition.split(PART_START);
						for (String part : parts) {
							VerticalPanel partPanel = new VerticalPanel();
							final String wordClass = part.substring(0,
									part.indexOf(PART_END));
							Label wordClassLabel = new Label(wordClass);
							partPanel.add(wordClassLabel);
							partPanel.add(new HTML("<br>"));

							String defPart = part.substring(part
									.indexOf(PART_END));
							String[] defs = defPart.split("<li>");
							for (String def : defs) {
								def = def.replaceAll("<[^>]+>", "").trim();
								if (def.equals(""))
									continue;
								int div1 = def.indexOf(')');
								int div2 = def.indexOf('(', div1);
								int div3 = def.lastIndexOf(')');

								VerticalPanel vp = new VerticalPanel();
								String synonyms = def.substring(div1 + 1, div2)
										.trim();
								final TextBox synonymsArea = new TextBox();
								if (!synonyms.equals("")) {
									synonymsArea.setStyleName("definitionArea");
									synonymsArea.setText(synonyms);
									vp.add(synonymsArea);
									synonymsArea.setWidth("100%");
								}

								String meaning = def.substring(div2 + 1, div3);
								final TextBox meaningArea = new TextBox();
								meaningArea.setStyleName("definitionArea");
								meaningArea.setText(meaning);
								vp.add(meaningArea);
								meaningArea.setWidth("100%");

								CheckBox cb = new CheckBox();
								cb.addClickHandler(new ClickHandler() {
									@Override
									public void onClick(ClickEvent event) {
										((CheckBox) event.getSource())
												.setEnabled(false);
										synonymsArea.setEnabled(false);
										meaningArea.setEnabled(false);
										String[] synonymsArray = synonymsArea
												.getText().split(",");
										ArrayList<String> synonyms = new ArrayList<String>(
												Arrays.asList(synonymsArray));
										String meaning = meaningArea.getText();
										Definition def = new Definition(word,
												wordClass, synonyms, meaning);
										definitions.add(def);
									}
								});

								HorizontalPanel hp = new HorizontalPanel();
								hp.add(cb);
								hp.add(vp);
								vp.setWidth("100%");

								partPanel.add(hp);
								hp.setWidth("100%");
								partPanel.add(new HTML("<br>"));
							}

							definitionBox.add(partPanel);
							partPanel.setWidth("100%");
							definitionBox.add(new HTML("<br>"));
						}
					} else {
						Window.alert("Dictionary failure!");
					}
				}
			});
		} catch (RequestException e) {
			Window.alert(e.getMessage());
		}

	}

	@Override
	public ArrayList<Definition> getAllDefinitions() {
		return definitions;
	}
}
